package com.lsk.admin;

import com.jfinal.core.Controller;
import com.jfinal.kit.PropKit;
import com.jfinal.plugin.activerecord.Record;
import com.jfinal.plugin.redis.Cache;
import com.jfinal.plugin.redis.Redis;
import com.lsk.admin.AdminSystem.AdminSystemService;

import com.lsk.util.AES;
import com.lsk.util.IPHelper;
import com.lsk.util.ReturnData;
import org.apache.http.HttpRequest;

import javax.servlet.http.HttpServletRequest;
import java.util.Enumeration;
import java.util.Map;

/**
 * Created by Administrator on 2017/12/11 0011.
 */
public class AdminLogin extends Controller {
    static AdminService adminService=new AdminService();
    static AdminSystemService adminSystemService=new AdminSystemService();
    Cache cache= Redis.use(PropKit.get("cache.name"));

    public void adminIndex(){
        String id= AES.AESDncode(getPara("id"));//sys_user表Id
        getSession().removeAttribute("userId");
        getSession().removeAttribute("title");
        getSession().removeAttribute("orgId");
        getSession().removeAttribute("rec");
        Record record=cache.get(id);
        String type=record.getStr("type");
        getSession().setAttribute("userType",type);
        ReturnData data = adminSystemService.findAllResource("user",id);//查询当前用户权限
        setAttr("data",data);
        setAttr("rec",record);

        getSession().setAttribute("userId",id);
        getSession().setAttribute("title","智慧营销管理系统");
        getSession().setAttribute("rec",record);
        String ip=IPHelper.getIpAddress(getRequest());
        LoginLogs loginLogs=new LoginLogs(new Record().set("userId",id).set("ip",ip));
        Thread thread=new Thread(loginLogs);
        thread.start();
        renderJsp("index.jsp");
    }

    /**
     * 登录验证
     */
    public void adminLogin(){
        Map<String,String[]> map=getParaMap();

        HttpServletRequest request=getRequest();
        StringBuffer url = request.getRequestURL();
        String tempContextUrl = url.delete(url.length() - request.getRequestURI().length(), url.length())
                .append(request.getSession().getServletContext().getContextPath()).append("/").toString();
//        if (tempContextUrl.contains("zzxw.19xw.cn")){
            ReturnData returnData=adminService.adminLogin(map);
            renderJson(returnData);
        /*}else {
//            ReturnData returnData=adminService.adminLogin(map);
//            renderJson(returnData);
            ReturnData returnData=new ReturnData(false);
            returnData.setMsg("非有效域名");
            renderJson(returnData);
        }*/

    }
    /**
     * 退出切换账户
     */
    public void quit(){
        Enumeration em =getSession().getAttributeNames();
        while(em.hasMoreElements()){
            getSession().removeAttribute(em.nextElement().toString());
        }
        redirect("/");
    }
    /**
     * 修改密码
     */
    public void updatePswd(){
        String id= getSession().getAttribute("userId").toString();
        String newPswd=getPara("newPswd");
        String pswd=getPara("pswd");
        ReturnData returnData=adminService.updatePswd( newPswd, pswd, id);
        renderJson(returnData);
    }
    /**
     * 去密码修改页面
     */
    public void passwordEdit(){
        renderJsp("updataPassword.jsp");
    }}
